package com.delong.spring.handler.market;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.delong.model.market.Pictures;
import com.delong.service.market.PicturesService;

@Controller
@RequestMapping("/banner")
public class BannerController {

	private PicturesService service = new PicturesService();

	@RequestMapping("/list")
	public String getappList(Map<String, Object> model, ServletRequest req) {
		HttpServletRequest request = (HttpServletRequest) req;
		model.put("username", request.getSession().getAttribute("username"));
		return "market/Banner";
	}

	@RequestMapping(value = "/getdata", method = RequestMethod.POST)
	@ResponseBody
	public String getPageListData(Map<String, Object> model, @RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "15") int rows) {

		page = page - 1;
		List<Pictures> elements = this.service.selectElementBanner(page * rows, rows);
		int total = this.service.selectElementBannerCount();
		JSONObject result = new JSONObject();
		result.put("rows", elements);
		result.put("total", total);
		return result.toJSONString();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String appAdd(Map<String, Object> model,
			@RequestParam(value = "title", defaultValue = "liyang") String title,
			@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "content_type", defaultValue = "0") int contentType,
			@RequestParam(value = "content_url", defaultValue = "0") String contentUrl,
			@RequestParam(value = "content_target", defaultValue = "0") String contentTarget

	) {
		Pictures element = new Pictures();
		element.setIntent_data(contentTarget);
		element.setIntent_type(contentType);
		element.setUrl(contentUrl);
		element.setTitle(title);
		// 分类中的应用数量增加

		if (id == 0) {
			element.setPic_type(2);
			element.setStatus(0);
			service.addElement(element);
		} else {

			element.setId(id);

			this.service.updateElement(element);
		}
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("message", "success");
		return result.toJSONString();
	}

	@RequestMapping("/changestatus")
	@ResponseBody
	public String changeAppElementStatus(Map<String, Object> model,
			@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "status", defaultValue = "0") int status) {

		JSONObject result = new JSONObject();
		if (id > 0) {
			result.put("code", 0);
			this.service.updateStatus(id, status);
		} else {
			result.put("code", 1);
		}

		return result.toJSONString();
	}

	@RequestMapping("/getelement")
	@ResponseBody
	public String getElement(Map<String, Object> model, @RequestParam(value = "id", defaultValue = "0") int id) {
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(id);
		List<Pictures> elements = this.service.getElementsByIds(ids);

		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("data", elements);
		return result.toJSONString();
	}

}
